package com.gxa.uhome.controller;

import com.gxa.uhome.exception.BusinessException;
import com.gxa.uhome.pojo.Station;
import com.gxa.uhome.service.LogService;
import com.gxa.uhome.service.StationService;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: fanyouzhao
 * @date: 11:54 2021/6/7
 */
@RestController
@CrossOrigin
@RequestMapping("/station")
@Api(tags = "Station站点操作接口", value="StationController",produces = "application/json;charset=UTF-8")
public class StationController {

    @Resource
    StationService stationService;
    @Resource
    LogService logService;
    JsonResult jsonResult = null;

    @ApiOperation(value = "添加站点")
    @PostMapping("/add")
    public JsonResult add(@RequestBody @ApiParam(name = "Station站点对象",value = "传入json格式") Station station, HttpSession httpSession) {
        Boolean add = stationService.add(station);
        Integer adminId = (Integer) httpSession.getAttribute("AdminId");
        if (add) {
            jsonResult = new JsonResult("1","添加成功");
            //如果添加成功，插入操作日志
            logService.insert("添加站点",adminId,"添加站点"+station.getName());
        } else {
            jsonResult = new JsonResult("0","添加失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "站点全查询")
    @GetMapping(value = "/selectAll", produces = "application/json;charset=UTF-8")
    public JsonResult selectAll() {
        List<Station> stations = stationService.selectAll();
        Integer count = stationService.getCount();
        jsonResult = new JsonResult("1","查询成功",stations);
        jsonResult.setValue("count",count);
        return jsonResult;
    }

    @ApiOperation(value = "站点分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="page",value="页码(默认为1)",required=false),
            @ApiImplicitParam(name="size",value="每页大小",required=true)
    })
    @GetMapping(value = "/selectByPage", produces = "application/json;charset=UTF-8")
    public JsonResult selectByPage(
            @RequestParam(required = false) Integer page,
            @RequestParam Integer size) {
        List<Station> stations = stationService.selectByPage(page, size);
        Integer count = stationService.getCount();
        jsonResult = new JsonResult("1","查询成功",stations);
        jsonResult.setValue("count",count);
        return jsonResult;
    }

    @ApiOperation(value = "站点模糊分页查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name="name",value="站点名称",required=false),
            @ApiImplicitParam(name="page",value="页码(默认为1)",required=false),
            @ApiImplicitParam(name="size",value="每页大小",required=true)
    })
    @GetMapping(value = "/selectLikeName", produces = "application/json;charset=UTF-8")
    public JsonResult selectLikeNameByPage(
            @RequestParam(required=false) String name,
            @RequestParam(required = false) Integer page,
            @RequestParam Integer size) {
        List<Station> stations = stationService.selectLikeNameByPage(name,page,size);
        Integer countLikeName = stationService.getCountLikeName(name);
        jsonResult = new JsonResult("1","查询成功",stations);
        jsonResult.setValue("count",countLikeName);
        return jsonResult;
    }

    @ApiOperation(value = "查询站点数量")
    @GetMapping(value = "/getCount", produces = "application/json;charset=UTF-8")
    public JsonResult getCount() {
        Integer count = stationService.getCount();
        jsonResult = new JsonResult("1","查询成功");
        jsonResult.setValue("count",count);
        return jsonResult;
    }

    @ApiOperation(value = "删除站点")
    @ApiImplicitParam(name="id",value="需要删除的站点的id",required=true)
    @PostMapping (value = "/del", produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public JsonResult del(@RequestBody Map<String,List<Integer>> id, HttpSession httpSession) {
        List<Integer> list = id.get("id");
        for (Integer integer : list) {
            Station station = stationService.selectById(integer);
            Boolean del = stationService.del(integer);
            Integer adminId = (Integer) httpSession.getAttribute("AdminId");
            if (del) {
                jsonResult = new JsonResult("1","删除成功");
                logService.insert("删除站点",adminId, "删除站点"+station.getName());
            } else {
                jsonResult = new JsonResult("0","删除失败");
            }
        }
        return jsonResult;
    }

    @ApiOperation(value = "测试")
    @PostMapping (value = "/del1", produces = "application/json;charset=UTF-8")
    @CrossOrigin
    public JsonResult del1() {
        JsonResult jsonResult = new JsonResult();
        Map<String,List<Integer>>  map = new HashMap<>();
        List<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(3);
        map.put("id",a);
        jsonResult.setData(a);
        return jsonResult;
    }



    @ApiOperation(value = "修改站点信息")
    @PostMapping("/update")
    public JsonResult update(@RequestBody @ApiParam(name = "Station站点对象",value = "传入json格式") Station station, HttpSession httpSession) {
        Boolean update = stationService.update(station);
        Integer adminId = (Integer) httpSession.getAttribute("AdminId");
        if (update) {
            jsonResult = new JsonResult("1","修改成功");
            logService.insert("修改站点",adminId,
                    "修改站点"+station.getName());
        } else {
            jsonResult = new JsonResult("0","修改失败");
        }
        return jsonResult;
    }
}
